package com.songshuai.webapi.permission.service.impl;

import com.songshuai.webapi.baseapi.dto.feign.response.NavAndFuncByRoleResDto;
import com.songshuai.webapi.baseapi.utils.PropertiesUtil;
import com.songshuai.webapi.permission.dao.*;
import com.songshuai.webapi.permission.dto.request.FunctionAddReqDto;
import com.songshuai.webapi.permission.dto.request.FunctionListReqDto;
import com.songshuai.webapi.permission.dto.request.FunctionUpdateReqDto;
import com.songshuai.webapi.permission.dto.response.FunctionListResDto;
import com.songshuai.webapi.permission.dto.response.FunctionsLoginResDto;
import com.songshuai.webapi.permission.model.YxFunctions;
import com.songshuai.webapi.permission.service.CurrentUserService;
import com.songshuai.webapi.permission.service.FunctionsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import java.util.stream.Collectors;

@Service
public class FunctionsServiceImpl implements FunctionsService {

    @Autowired
    private YxFunctionsMapper functionsMapper;

    @Autowired
    private YxNavigationFunctionMapper navigationFunctionMapper;

    @Autowired
    private YxUserNavigationFunctionMapper userNavigationFunctionMapper;

    @Autowired
    private CurrentUserService currentUserService;

    @Autowired
    private YxRolesNavigationsMapper rolesNavigationsMapper;

    @Autowired
    private YxRoleNavigationFunctionMapper roleNavigationFunctionMapper;



    @Override
    public List<FunctionsLoginResDto> queryLoginFunctionList(Integer appId) {
        //根据appId查询功能列表
        List<YxFunctions> uniqueFuncList = new ArrayList<>();
        if(currentUserService.CheckIsSysAdmin()){//如果是超级管理员
            uniqueFuncList = functionsMapper.queryLoginAdminFuncList(appId);
        } else {//如果是普通用户
            List<YxFunctions> functionsList = functionsMapper.queryLoginFunctionList(currentUserService.getUserId(), appId);
            //去重
            uniqueFuncList = functionsList.stream().collect(
                    Collectors.collectingAndThen(
                            Collectors.toCollection(() -> new TreeSet<>(Comparator.comparing(YxFunctions::getFunctionId))), ArrayList::new)
            );
        }

        //类转换
        List<FunctionsLoginResDto> resDtoList = uniqueFuncList.stream().map(functions -> {
            FunctionsLoginResDto resDto = new FunctionsLoginResDto();
            PropertiesUtil.copyProperties(functions,resDto);
            return resDto;
        }).collect(Collectors.toList());
        return resDtoList;
    }

    @Override
    public List<FunctionListResDto> queryFunctionListByPage(FunctionListReqDto parmDto) {
        //根据appId和功能名字查询功能列表
        List<YxFunctions> functionsList = functionsMapper.queryFunctionListByPage(parmDto);
        //类转换
        List<FunctionListResDto> resDtoList = functionsList.stream().map(functions -> {
            FunctionListResDto resDto = new FunctionListResDto();
            PropertiesUtil.copyProperties(functions,resDto);
            return resDto;
        }).collect(Collectors.toList());
        return resDtoList;
    }

    @Override
    public YxFunctions queryFunctionByKey(String functionKey) {
        return functionsMapper.queryFunctionByKey(functionKey);
    }

    @Override
    public boolean addFunction(FunctionAddReqDto parmDto) {
        YxFunctions entity = new YxFunctions();
        PropertiesUtil.copyProperties(parmDto, entity);
        //获取当前最大功能id
        int maxFuncId = functionsMapper.getMaxFuncId();
        entity.setFunctionId(maxFuncId + 1);
        entity.setCreateBy(currentUserService.getUserId());
        entity.setUpdateBy(currentUserService.getUserId());
        entity.setCreateTime(new Date());
        entity.setUpdateTime(new Date());
        return functionsMapper.addFunction(entity) > 0;
    }

    @Override
    public boolean updateFunction(FunctionUpdateReqDto parmDto) {
        YxFunctions entity = new YxFunctions();
        PropertiesUtil.copyProperties(parmDto, entity);
        entity.setUpdateBy(currentUserService.getUserId());
        entity.setUpdateTime(new Date());
        return functionsMapper.updateFunction(entity) > 0;
    }

    @Override
    @Transactional
    public boolean deleteFunction(Integer functionId) {
        //删除菜单功能关联
        navigationFunctionMapper.deleteFuncNavRelation(functionId);
        //删除用户菜单功能关联
        userNavigationFunctionMapper.deleteFuncUserNavRelation(functionId);
        //删除角色菜单功能关联
        functionsMapper.deleteFuncRoleNavRelation(functionId);
        //删除功能
        return functionsMapper.deleteFunction(functionId) > 0;
    }

    @Override
    public List<NavAndFuncByRoleResDto> queryFunctionKeyByRole(Integer roleId) {
        List<NavAndFuncByRoleResDto> navIds = new ArrayList<>();
        List<Integer> list = rolesNavigationsMapper.queryNavByRole(roleId);//菜单集合
        for (Integer id : list) {
            NavAndFuncByRoleResDto resDto = new NavAndFuncByRoleResDto();
            resDto.setNavigationId(id);
            resDto.setFunctionIds(roleNavigationFunctionMapper.getFuncIdByRoleAndNav(roleId,id));//功能集合
            navIds.add(resDto);
        }
        return navIds;
    }

    @Override
    public YxFunctions queryFunctionByFuncId(Integer functionId) {
        return functionsMapper.queryFunctionByFuncId(functionId);
    }


}